<template>
	<view>
		<my-search></my-search>
		<view class="scroll-view-container">
			<!-- 左侧滑动区域 -->
			<scroll-view class="left-scroll-view" scroll-y="true" :style="{height:wh+'px'}">
				<view class="left-scroll-view-item" :class="{active: idx == active}" v-for="(item,idx) in cateList" :key="idx" @click="activeChanged(idx)">{{item.cat_name}}</view>
			</scroll-view>
			<!-- 右侧滑动区域 -->
			<scroll-view scroll-y="true" :style="{height:wh+'px'}" :scroll-top="scrollTop">
				<view class="cate-lv2" v-for="(item,idx) in cateLevel2" :key="idx">
					<view class="cate-lv2-title">
						/ {{item.cat_name}} /
					</view>
					<view class="cate-lv3-list">
						<view class="cate-lv3-item" v-for="(item1, idx1) in item.children" :key="idx1" @click="goToGoodsList(item1)">
							<image :src="item1.cat_icon" mode=""></image>
							<text>{{item1.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from "@/mixins/tabbar-badge.js"
	export default {
		mixins: [badgeMix],
		data() {
			return {
				//当前设备可用高度
				wh: 0,
				//分类列表的数据
				cateList: [],
				active: 0,
				//二级分类列表
				cateLevel2: [],
				scrollTop: 0
			};
		},
		
		onLoad() {
			//调用api获取当前设备信息(例如设备名称,系统版本,屏幕大小等)
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight - 50
			this.getCateList()
		},
		methods: {
			goToGoodsList(item) {
				uni.navigateTo({
					url: "/subpkg/goods_list/goods_list?cid=" + item.cat_id
				})
			},
			activeChanged(idx) {
				this.scrollTop = this.scrollTop == 0 ? 1 : 0
				this.active = idx
				//重新为一级分类赋值
				this.cateLevel2 = this.cateList[idx].children
			},
			//获取分类列表的数据
			async getCateList() {
				const res = await uni.$http.get("/api/public/v1/categories")
				if(res.data.meta.status == 200) {
					this.cateList = res.data.message
					this.cateLevel2 = res.data.message[0].children
				} else {
					uni.$showMsg()
				}
				console.log(res)
			}
		}
	}
</script>

<style lang="scss">
.scroll-view-container {
	display: flex;
	.left-scroll-view {
		width: 120px;
		.left-scroll-view-item {
			background-color: #f7f7f7;
			line-height: 60px;
			text-align: center;
			font-size: 12px;
			&.active {
				background-color: #fff;
				position: relative;
				&:before {
					content: "";
					display: block;
					width: 3px;
					height: 30px;
					background-color: #C00000;
					position: absolute;
					top: 50%;
					transform: translate(0, -50%);
				}
			}
		}
	}
}
.cate-lv2-title {
	font-size: 12px;
	font-weight: bold;
	text-align: center;
	padding: 15px 0;
}
.cate-lv3-list {
	display: flex;
	flex-wrap: wrap;
	.cate-lv3-item {
		margin-bottom: 10px;
		width: 33.33%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		image {
			width: 70px;
			height: 60px;
		}
		text {
			font-size: 12px;
		}
	}
}
</style>
